package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.AdminDivisionMapper;
import com.link_with_health.mapper.DepartmentsMapper;
import com.link_with_health.model.DepartmentsEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.DepartmentsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("departmentsService")
public class DepartmentsServiceImpl extends BaseService implements DepartmentsService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    DepartmentsMapper departmentsMapper;

    /**
     * 通过id获取科室信息
     * @param departmentId 科室id
     * @return 返回科室信息
     * @throws BaseException 异常
     */
    @Override
    public DepartmentsEntity getDepartById(Long departmentId) throws BaseException {
        DepartmentsEntity entity = null;
        try {
            entity = departmentsMapper.queryDepartById(departmentId);
        } catch (Exception e) {
            logger.error("[query department failed]",e);
        }
        if(entity == null)
            throw new NullQueryResultException("DEPARTMENTS.DEPARTMENT_ID: "+departmentId);
        return entity;
    }

    /**
     * 获取主科室信息
     * @return 主科室信息
     * @throws BaseException 异常
     */
    @Override
    public List<DepartmentsEntity> getMainDepartments() throws BaseException {
        List<DepartmentsEntity> departList = null;
        try {
            departList = departmentsMapper.queryMainDepartments();
        } catch (Exception e) {
            logger.error("[query departments failed]", e);
        }
        if(departList == null || departList.isEmpty())
            throw new NullQueryResultException("PARENT DEPART IN DEPARTMENTS");

        return departList;
    }

    /**
     * 通过上级科室id获取其下的分支科室
     * @param parentDepart 上级科室id
     * @return 分支科室信息
     * @throws BaseException 异常
     */
    @Override
    public List<DepartmentsEntity> getDepartByParentDepart(int parentDepart)
            throws BaseException {
         List<DepartmentsEntity> departList = null;
         try {
             departList = departmentsMapper
                     .queryDepartmentsByParentDepart(parentDepart);
         } catch (Exception e) {
             logger.error("[query departments failed]", e);
         }

         if(departList == null || departList.isEmpty())
             throw new NullQueryResultException
                     ("CHILD DEPART IN DEPARTMENTS "+parentDepart);

         return departList;
    }

    @Override
    public DepartmentsEntity getDepartByNameExact(String name)
            throws BaseException{
        DepartmentsEntity entity = null;
        try {
            entity = departmentsMapper.queryDepartByNameExact(name);
        } catch (Exception e) {
            logger.error("[query department failed]",e);
        }
        if(entity == null)
            throw new NullQueryResultException("DEPARTMENTS.DEPARTMENT: "+name);
        return entity;
    }

    /**
     * 获取所有科室信息
     * @return 科室信息
     * @throws BaseException 异常
     */
    @Override
    public List<DepartmentsEntity> getAllDepartments() throws BaseException {
        List<DepartmentsEntity> departList = null;
        try {
            departList = departmentsMapper.queryAllDepartments();
        } catch (Exception e){
            logger.error("[query departments failed]",e);
        }
        if(departList == null || departList.isEmpty())
            throw new NullQueryResultException("ALL DEPARTMENTS");
        return departList;
    }

    /**
     * 增加科室信息
     * @param department 科室信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addDepartment(DepartmentsEntity department) throws BaseException{
        int result = 0;
        try {
            result = departmentsMapper.insertDepartment(department);
        } catch (Exception e) {
            logger.error("[add departments failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT DEPARTMENTS: "+department.toString());

        return result;
    }

    /**
     * 更改科室信息
     * @param department 科室信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeDepartment(DepartmentsEntity department) throws BaseException{
        int result = 0;
        try {
            result = departmentsMapper.updateDepartment(department);
        } catch (Exception e) {
            logger.error("[change departments]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("UPDATE DEPARTMENTS: "+department.toString());
        return result;
    }

    /**
     * 删除科室信息
     * @param departmentId 科室id
     * @return
     * @throws BaseException 异常
     */
    @Override
    public int deleteDepartment(Long departmentId) throws BaseException{
        int result = 0;
        try {
            result = departmentsMapper.deleteDepartment(departmentId);
        } catch (Exception e) {
            logger.error("[delete departments failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("DELETE DEPARTMENTS: "+departmentId);

        return result;
    }
}
